package com.example.teamtribe.main.club.clubintroduce.media;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.PopupPics;
import com.jph.takephoto.model.TResult;

public class AddMediaActivity extends PhotoActivity implements AddMediaContract.View,View.OnClickListener,PopupPics.OnPickListener{
    private int teamId;
    private int type;
    private String cover;
    private PopupPics popup;
    private AddMediaContract.Presenter presenter;

    private ImageView ivCover;
    private TextView mediaName;
    private TextView mediaDescription;
    private ImageView done;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_addmedia);
        teamId=getIntent().getIntExtra("teamId",-1);
        type=getIntent().getIntExtra("type",-1);
        if(teamId==-1||type==-1)finish();
        ivCover=(ImageView)findViewById(R.id.iv_cover);
        mediaName=(TextView)findViewById(R.id.et_name);
        mediaDescription=(TextView)findViewById(R.id.et_description);

        if(type==1){
            ((TextView)findViewById(R.id.tv_title)).setText("新建相册");
        }else
            ((TextView)findViewById(R.id.tv_title)).setText("新建影集");

        done=(ImageView)findViewById(R.id.iv_more);
        done.setImageResource(R.drawable.ic_done);
        done.setVisibility(View.VISIBLE);
        done.setOnClickListener(this);
        ivCover.setOnClickListener(this);
        Glide.with(this).load(R.drawable.cover_default).into(ivCover);
        popup = new PopupPics(this, this);
        presenter = new AddMediaPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cover:
                popup.show(v);
                break;

            case R.id.iv_more:
                    presenter.addMedia(mediaName.getText().toString().trim(),teamId, cover, mediaDescription.getText().toString().trim(),type);
                    break;

            default:
                break;
        }
    }
    @Override
    public void next() {
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        String path;
        if (result.getImage().isCompressed()) {
            path = result.getImage().getCompressPath();
        } else {
            path = result.getImage().getOriginalPath();
        }
        cover = path;
        Glide.with(this).load(cover).into(ivCover);
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), getCropOptions());
    }
}
